﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Modelica.Parsers;
using Modelica.Annotations;
using Modelica.CoreLite;
using Modelica.ModelicaTypes;

namespace ParserTestFrontEnd
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1() {
            InitializeComponent();
        }

        private void parseButton_Click(object sender, RoutedEventArgs e)
        {
            // tu si ulozime naparsovanu ikonu
            Icon parsedIcon = null;

            this.parseOutputTextBox.Text = String.Empty;
            this.astTextBox.Text = String.Empty;

            try{                
                // zavolam metodu na parsovanie s parametrom string,
                // v ktorom je anotacia
                // bud dostanem ikonku, alebo vynimku Excpetion
                parsedIcon = AnnotationParser.ParseIconAnnotation(this.textToParseTextBox.Text);
            }catch (Exception excp) {
                // vynimka ma ako message string s nejakym blabolom typu "nepodarilo sa naparsovat anotaciu"
                // a poziciou chyby - to by mozno bolo lepsie aj nejako inak predat
                // ako inner exception je vynimka z parseru, vacsinou nehovori nic extra, ale to je chyba parseru
                this.parseOutputTextBox.Text = excp.Message + System.Environment.NewLine + excp.InnerException.Message;
            }
            // no a tuna mame naparsovanu ikonku
            if (parsedIcon != null) {
                this.astTextBox.Text = AnnotationParser.CheckIcon(parsedIcon);
            }
        }

        private void clearButton_Click( object sender, RoutedEventArgs e ) {
            this.parseOutputTextBox.Text = String.Empty;
            this.astTextBox.Text = String.Empty;
            this.textToParseTextBox.Text = String.Empty;
        }

        private void loadButton_Click( object sender, RoutedEventArgs e ) {
            try {
                // natiahneme do OMC nejaky model
                bool success = Core.LoadModel(modelPathTextBox.Text,"");
                if ( success == false ) {
                    throw new Exception("Load model from file failed");
                }

            } catch ( Exception excp ) {
                topClassesTextBox.Text = excp.Message;
            }
            // len tak pre zaujimavost odmeriame ako dlho trva nacitanie modelu
            System.DateTime dt = System.DateTime.Now;

            // zavolame Core.getModelStructure, ktory vrati celu strukturu modelu,
            // top level elemnty tvoria List<ModelicaClass>, kazda ModelicClass
            // (a aj triedy dediace z nej) ma zas list jej podelementov
            List<ModelicaClass> topClasses = Core.GetModelStructure();
            timeTextBox.Text = "Model structure created in " + (System.DateTime.Now - dt).TotalSeconds.ToString() + " seconds.";
            
            // toto je len taka obalovacia trieda pre ten zoznam, az nevysi len tak vo vzduchu
            // potrebna aby som mohol lahko zavolat modelToString
            // bude ale treba asi definovat nejaky korenovy element, nech je jasne, kam sa vytvorena struktura modelu ulozi
            ModelicaClass mClass = new ModelicaClass("RootClass",new Diagram(),new Icon());
            mClass.ContainedClasses = topClasses;

            // len testovacia funkcia, ktora vypise triedy v strukture modelu
            string modelStructure = Core.ModelToString(mClass);            
            topClassesTextBox.Text = modelStructure;
        }
    }
}
